class Solution {
    public int minOperations(int[] nums, int x) {
        int left = 0;
        int right = 0;
        int sum = 0;
        //求整个数组的和
        for (int num : nums) {
            sum += num;
        }
        if(sum < x){
            return -1;
        }
        int len = -1;
        int subSum = 0;
        while(right < nums.length){
            //进窗口
            subSum += nums[right];
            //出窗口
            while(subSum > sum - x){
                subSum -= nums[left];
                left++;
            }
            if(subSum == sum - x){
                //更新结果
                if(len < right - left + 1){
                    len = right - left + 1;
                }
            }
            right++;
        }
        return len == -1 ? -1 : nums.length - len;
    }
}